#! /usr/bin/sdlbrt
finp$="picture.png"
setdisplay(64,48,32,1):paper(8^8-1):ink(0):pen(0):cls
loadimage(finp$,1):pasteicon(0,0,1)

function rgb2idmsx1(tclr1)
  tclr1b=tclr1
  tb1=(tclr1b mod 256):tclr1b=int(tclr1b/256)
  tg1=(tclr1b mod 256):tclr1b=int(tclr1b/256)
  tr1=(tclr1b mod 256)
  '---- getting msx colour space from rgb

  'tms9918
  tpindx= 1

  'greens:
  if tg1>128 and tr1<128 and tb1<128 then
    tpindx=2
    if tg1>192 then: tpindx=2 :end if
    if tg1<160 then: tpindx=12:end if
    end if

  'blues and cyan:
  if tb1>220 and tr1<140 and tg1<220 then
    tpindx=5
    if tg1<100 then: tpindx=4 :end if
    if tg1>190 then: tpindx=7 :end if
    end if

  'reds:
  if tr1>150 and tg1<150 and tb1<110 then
    tpindx=8
    if tr1<190 then: tpindx=6 :end if
    if tr1>220 then: tpindx=9 :end if
    end if

  'yellows:
  if tr1>180 and tg1>190 and tb1<120 then
    tpindx=10
    if tr1>200 then: tpindx=11 :end if
    end if

  'magenta:
  if tr1>128 and tg1<128 and tb1>128 then
    tpindx=13
    end if

  'grey and white:
  if tr1>190 and tg1>190 and tb1>190 then
    tpindx=14
    if tr1>210 then: tpindx=15 :end if
    end if


  'tpindx= 9:                                  tpindx= 9
  ''g128,r128
  'if              tg1>=128              then: tpindx= 7:end if
  'if tr1<128  and tg1<128               then: tpindx= 1:end if
  'if tr1>=128 and tg1>=128              then: tpindx=14:end if
  ''b128
  'if tr1<128  and tg1<128  and tb1>=128 then: tpindx= 5:end if
  'if tr1>=128 and tg1<128  and tb1>=128 then: tpindx=13:end if
  'if tr1<128  and tg1>=128 and tb1<128  then: tpindx=12:end if
  'if tr1>=128 and tg1>=128 and tb1<128  then: tpindx=10:end if
  ''g64
  'if tr1<128  and tg1<64   and tb1>=128 then: tpindx= 4:end if
  'if tr1>=128 and tg1<64   and tb1<128  then: tpindx= 6:end if
  'if tr1<128  and tg1>=192 and tb1<128  then: tpindx= 3:end if
  'if tr1>=128 and tg1>=192 and tb1>=128 then: tpindx=11:end if
  ''r64,b64
  'if tr1>=192 and tg1<64   and tb1<128  then: tpindx= 8:end if
  'if tr1<64   and tg1>=192 and tb1<128  then: tpindx= 2:end if
  'if tr1>=128 and tg1>=210 and tb1>=210 then: tpindx=15:end if

  ''tms9938
  'tpindx= 9:                                  tpindx= 9
  ''g128,r128
  'if              tg1>=128              then: tpindx= 7:end if
  'if tr1<128  and tg1<128               then: tpindx= 1:end if
  'if tr1>=128 and tg1>=128              then: tpindx=14:end if
  ''b128
  'if tr1<128  and tg1<128  and tb1>=128 then: tpindx= 5:end if
  'if tr1>=128 and tg1<128  and tb1>=128 then: tpindx=13:end if
  'if tr1<128  and tg1>=128 and tb1<128  then: tpindx=12:end if
  'if tr1>=128 and tg1>=128 and tb1<128  then: tpindx=10:end if
  ''g64
  'if tr1<128  and tg1<64   and tb1>=128 then: tpindx= 4:end if
  'if tr1>=128 and tg1<64   and tb1<128  then: tpindx= 6:end if
  'if tr1<128  and tg1>=192 and tb1<128  then: tpindx= 3:end if
  'if tr1>=128 and tg1>=192 and tb1>=128 then: tpindx=11:end if
  ''r64,b64
  'if tr1>=192 and tg1<64   and tb1<128  then: tpindx= 8:end if
  'if tr1<64   and tg1>=192 and tb1<128  then: tpindx= 2:end if
  'if tr1>=128 and tg1>=192 and tb1>=192 then: tpindx=15:end if

  return (tpindx mod 16)
  end function

open finp$+".bin" for output as #1

for y1=0 to 5
  for x1=0 to 63 step 2
    for y2=0 to 7
      y=(y1*8)+y2
      u1=rgb2idmsx1(point(x1,y))
      u2=rgb2idmsx1(point(x1+1,y))
      u3=(u1*16)+u2
      writebyte (1,u3)
      next:next:next

close #1

